package com.furns.web;

import com.furns.entity.*;
import com.furns.service.OrderService;
import com.furns.service.impl.OrderServiceImpl;
import com.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Author LuoJiaCheng
 * @ID 2240129126
 * @Version 1.0
 */
@WebServlet(name = "OrderServlet", value = "/OrderServlet")
public class OrderServlet extends BasicServlet{
    private OrderService orderService = new OrderServiceImpl();

    //生成订单和订单项，跳转到生成订单成功界面
    protected void saveOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车
        Cart cart =(Cart)req.getSession().getAttribute("cart");
        if (null==cart){
            req.getRequestDispatcher("/").forward(req,resp);
            return;
        }
        //获取当前的memberId
        Member member =(Member) req.getSession().getAttribute("member");
        if(null==member){
            req.getRequestDispatcher("/views/member/login.jsp").forward(req,resp);
            return;
        }
        //生成订单
        String orderId = orderService.saveOrder(cart, member.getId());
        req.getSession().setAttribute("orderId",orderId);


        //跳转结账成功页面
        resp.sendRedirect(req.getContextPath()+"/views/order/checkout.jsp");
    }


    protected void showOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查找到生成的订单号
        String orderId = (String) req.getSession().getAttribute("orderId");

        //根据订单号返回订单
        Order order = orderService.showOrder(orderId);
       // System.out.println(order);
      //  System.out.println(orderId);
        //存入session阈中
        req.getSession().setAttribute("order",order);
        //跳转到显示订单页面
        req.getRequestDispatcher("/views/order/order.jsp").forward(req,resp);
    }

    protected void showOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查找到生成的订单号
        String orderId = (String) req.getSession().getAttribute("orderId");

        //根据订单号返回订单
        List<OrderItem> orderItems = orderService.showOrderItem(orderId);

        //存入session阈中
        req.getSession().setAttribute("orderItems",orderItems);
        //跳转到显示订单页面
        req.getRequestDispatcher("/views/order/order_detail.jsp").forward(req,resp);
    }

    //管理员账单分页
    protected void pageByOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        System.out.println(pageSize);
        //调用service对象获取Page对象
        Page<Order> page = orderService.page(pageNo, pageSize);
        req.setAttribute("pageByOrder",page);
        req.getRequestDispatcher("/views/manage/manage_order.jsp").forward(req,resp);
    }

    protected void orderItemById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查找到当前的订单号
        String orderId = req.getParameter("orderId");

        //根据订单号返回订单
        List<OrderItem> orderItems = orderService.showOrderItem(orderId);

        //存入session阈中
        req.getSession().setAttribute("orderItemsById",orderItems);
        //保存来时的界面
        String referer = req.getHeader("Referer");
        req.getSession().setAttribute("referer",referer);
        req.getSession().setAttribute("manageOrderId",orderId);
        //跳转到显示订单页面
        req.getRequestDispatcher("/views/manage/manage_order_detail.jsp").forward(req,resp);
    }

    //管理员账单分页
    protected void pageByOrderId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int memberId = DataUtils.parseInt(req.getParameter("memberId"), 1);
        System.out.println(memberId);
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //调用service对象获取Page对象
        Page<Order> page = orderService.page(memberId, pageNo, pageSize);
        req.setAttribute("pageByOrderId",page);
        req.getRequestDispatcher("/views/order/member_order.jsp").forward(req,resp);
    }
}
